package de.justphil.tcg.tcgserver.rest.resources.helpers;

import java.util.Map;

import org.slf4j.Logger;

import de.justphil.tcg.tcgserver.commons.domain.Card;
import de.justphil.tcg.tcgserver.commons.domain.CardInstance;
import de.justphil.tcg.tcgserver.commons.domain.containers.NewGame;
import de.justphil.tcg.tcgserver.commons.exceptions.TCGException;
import de.justphil.tcg.tcgserver.commons.management.DataManager;
import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.*;

public class GameHelper {
	private static final Logger log = restLogger(GameHelper.class);
	
	public static void populateNewGameWithInGameCards(DataManager dataManager, NewGame newGame) throws TCGException {
		log.debug( "populateNewGameWithInGameCards()" );
		
		Map<Long, Card> inGameCards = newGame.getInGameCards();
		
		Card c = null;
		
		for (int i = 0; i < newGame.getDecks().length; i++) {
			
			for (CardInstance ci : newGame.getDecks()[i].getCollection()) {
				
				if (!inGameCards.containsKey( ci.getCardId() )) {
					
					c = dataManager.getCardById( ci.getCardId() );
					
					inGameCards.put(ci.getCardId(), c);
					
					log.debug("Added an in-game card ({}, {}) for card instance {}!",
							new Object[] { c.getId(), c.getName(), ci.getId() }
					);
				}
				
			}
			
		}
	}

}
